package com.ocean.redflagtest.mvvm.models.sql

import android.arch.persistence.room.Database
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context

import com.ocean.redflagtest.mvvm.models.entity.OriginalModel

/**
 * Created by ocean on 2018/8/2
 * Author :  ocean
 * Email  :  348686686@qq.com
 */

@Database(entities = arrayOf(OriginalModel::class), version = 2, exportSchema = false)
abstract class OriginalRoomDatabase : RoomDatabase() {

    /**
     * 数据访问接口
     */
    abstract fun originalModelDao(): OriginalModelDao

    companion object {

        /**
         * 获取单例
         * 使用dao接口
         */
        private var INSTANCE: OriginalRoomDatabase? = null

        fun getINSTANCE(context: Context): OriginalRoomDatabase {
            if (INSTANCE == null) {
                synchronized(OriginalRoomDatabase::class.java) {
                    if (INSTANCE == null) {
                        INSTANCE = Room.databaseBuilder(context.applicationContext,
                                OriginalRoomDatabase::class.java, "red_flag_db")
                                .fallbackToDestructiveMigration()
                                .build()
                    }
                }
            }
            return INSTANCE!!
        }
    }

}
